/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com 
 * @author: Yannis   
 * @date: 2019年9月25日 上午11:35:05 
 */
package com;

import com.alibaba.fastjson.JSONObject;
import com.tce.cvm.CVMClient;
import com.tce.cvm.DescribeInstancesResponse;
import com.tce.cvm.DescribeInstancesStatusResponse;
import com.tce.cvm.RunInstancesResponse;
import com.tce.tke.DescribeClustersResponse;
import com.tce.tsfcluster.AddInstancesRequest;
import com.tce.tsfcluster.AddInstancesResponse;
import com.tce.tsfcluster.TsfClient;
import com.vpc.Subnet;
import com.vpc.VpcUtils;
import com.tce.model.Cluster;

public class Demo {
	
	public static void main(String[] args) {

		//添加CVM instance至tsfCluster
		
		
		// 1. 获取指定vpc的子网ID
		String vpcId = "vpc-omskc9f0";
		String region = "bj";
		String zone = "kc";
		if("1".equals(args[0])) {
			Subnet subnet = null;
			try {
				subnet = VpcUtils.getFitSubNetByVpc(vpcId, region, zone);
				System.out.println("subnet === " + subnet.getSubnetId() + "----- " + subnet.getSubnetName());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return;
			}
		}
		
		String subnetId ="subnet-pdvk3065";
		String clusterName = "test-cluster";
//		try {
//			// 2. 创建集群
//			CreateClusterRequest request = new CreateClusterRequest();
//			request.setClusterCIDR("172.18.0.0/16");
//			request.setClusterName(clusterName);
//			request.setClusterType("C");
//			request.setSubnetId(subnetId);
//			request.setVpcId(vpcId);
//			CreateClusterResponse response = TsfClient.createCluster(request, region, "tsf.api3.tce.piccit.com.cn");
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			return;
//		}
		
		// 获取集群详情
		String clusterId = "";
		try {
			String tkeUrl = "ccs.api2.tce.piccit.com.cn/v2/index.php";
			while(true) {
				DescribeClustersResponse describeClustersResponse = TsfClient.getClusters(clusterName, region, tkeUrl);
				Cluster cluster = describeClustersResponse.getClusters()[0];
				if("Running".equalsIgnoreCase(cluster.getStatus())) {
					clusterId = cluster.getClusterId();
					break;
				}
			}
			System.out.println("=================");
			System.out.println("clusterId------------------>>>" + clusterId);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
//		// 创建CVM
//        try {
//        	RunInstancesResponse cvmResponse = CVMClient.createCVMResponse();
//        	System.out.println("==========");
//        	
//        	System.out.println(JSONObject.toJSONString(cvmResponse));
//		} catch (Exception e2) {
//			e2.printStackTrace();
//			return;
//		}
//		//查看CVM列表
		String[] instanceIds = {"ins-bfxtt2r9","ins-5lt3h1h3"};
//        try {
//			DescribeInstancesResponse instanceList = CVMClient.getCvmInstances(instanceIds);
//			System.out.println(JSONObject.toJSONString(instanceList));
//		} catch (Exception e2) {
//			e2.printStackTrace();
//			return;
//		}
//        
//		//查看CVM状态列表
//		try {
//			DescribeInstancesStatusResponse instanceStatusResponse = CVMClient.getCvmInstancesStatus(instanceIds);
//			System.out.println(JSONObject.toJSONString(instanceStatusResponse));
//		} catch (Exception e2) {
//			e2.printStackTrace();
//		}
//		
		
		// add instances to cluster
		
		try {
			AddInstancesRequest addInstancesRequest = new AddInstancesRequest();
			addInstancesRequest.setClusterId(clusterId);
			addInstancesRequest.setImageId("img-8toqc6s3");
			addInstancesRequest.setInstanceIdList(instanceIds);
			addInstancesRequest.setPassword("Asd@1234");
			AddInstancesResponse addInstancesResponse = TsfClient.addInstancesToCluster(addInstancesRequest, region);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
//		try {
//			TsfClient.deleteCluster(clusterId, region, "tsf.api3.tce.piccit.com.cn");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return;
//		}
	}
}
